<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Calculation of the Type of the Result</title>
<link rel="stylesheet" href="../math.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="Math Toolkit 4.1.0">
<link rel="up" href="../overview.html" title="Chapter 1. Overview">
<link rel="prev" href="standalone.html" title="Standalone Usage">
<link rel="next" href="error_handling.html" title="Error Handling">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
<td align="center"><a href="../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="standalone.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../overview.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="error_handling.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="math_toolkit.result_type"></a><a class="link" href="result_type.html" title="Calculation of the Type of the Result">Calculation of the Type of the
    Result</a>
</h2></div></div></div>
<p>
      The functions in this library are all overloaded to accept mixed floating point
      (or mixed integer and floating point type) arguments. So for example:
    </p>
<pre class="programlisting"><span class="identifier">foo</span><span class="special">(</span><span class="number">1.0</span><span class="special">,</span> <span class="number">2.0</span><span class="special">);</span>
<span class="identifier">foo</span><span class="special">(</span><span class="number">1.0f</span><span class="special">,</span> <span class="number">2</span><span class="special">);</span>
<span class="identifier">foo</span><span class="special">(</span><span class="number">1.0</span><span class="special">,</span> <span class="number">2L</span><span class="special">);</span>
</pre>
<p>
      etc, are all valid calls, as long as "foo" is a function taking two
      floating-point arguments. But that leaves the question:
    </p>
<div class="blurb">
<div class="titlepage"><div><div><p class="title"><b></b></p></div></div></div>
<p>
      <span class="emphasis"><em>"Given a special function with N arguments of types T1, T2,
      T3 ... TN, then what type is the result?"</em></span>
    </p>
</div>
<p>
      <span class="bold"><strong>If all the arguments are of the same (floating point)
      type then the result is the same type as the arguments.</strong></span>
    </p>
<p>
      Otherwise, the type of the result is computed using the following logic:
    </p>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem">
          Any arguments that are not template arguments are disregarded from further
          analysis.
        </li>
<li class="listitem">
          For each type in the argument list, if that type is an integer type then
          it is treated as if it were of type <code class="computeroutput"><span class="keyword">double</span></code>
          for the purposes of further analysis.
        </li>
<li class="listitem">
          If any of the arguments is a user-defined class type, then the result type
          is the first such class type that is constructible from all of the other
          argument types.
        </li>
<li class="listitem">
          If any of the arguments is of type <code class="computeroutput"><span class="keyword">long</span>
          <span class="keyword">double</span></code>, then the result is of type
          <code class="computeroutput"><span class="keyword">long</span> <span class="keyword">double</span></code>.
        </li>
<li class="listitem">
          If any of the arguments is of type <code class="computeroutput"><span class="keyword">double</span></code>,
          then the result is of type <code class="computeroutput"><span class="keyword">double</span></code>.
        </li>
<li class="listitem">
          Otherwise the result is of type <code class="computeroutput"><span class="keyword">float</span></code>.
        </li>
</ol></div>
<p>
      For example:
    </p>
<pre class="programlisting"><span class="identifier">cyl_bessel</span><span class="special">(</span><span class="number">2</span><span class="special">,</span> <span class="number">3.0</span><span class="special">);</span>
</pre>
<p>
      Returns a <code class="computeroutput"><span class="keyword">double</span></code> result, as does:
    </p>
<pre class="programlisting"><span class="identifier">cyl_bessel</span><span class="special">(</span><span class="number">2</span><span class="special">,</span> <span class="number">3.0f</span><span class="special">);</span>
</pre>
<p>
      as in this case the integer first argument is treated as a <code class="computeroutput"><span class="keyword">double</span></code>
      and takes precedence over the <code class="computeroutput"><span class="keyword">float</span></code>
      second argument. To get a <code class="computeroutput"><span class="keyword">float</span></code>
      result we would need all the arguments to be of type float:
    </p>
<pre class="programlisting"><span class="identifier">cyl_bessel_j</span><span class="special">(</span><span class="number">2.0f</span><span class="special">,</span> <span class="number">3.0f</span><span class="special">);</span>
</pre>
<p>
      When one or more of the arguments is not a template argument then it doesn't
      effect the return type at all, for example:
    </p>
<pre class="programlisting"><span class="identifier">sph_bessel</span><span class="special">(</span><span class="number">2</span><span class="special">,</span> <span class="number">3.0f</span><span class="special">);</span>
</pre>
<p>
      returns a <code class="computeroutput"><span class="keyword">float</span></code>, since the first
      argument is not a template argument and so doesn't effect the result: without
      this rule functions that take explicitly integer arguments could never return
      <code class="computeroutput"><span class="keyword">float</span></code>.
    </p>
<p>
      And for user-defined types, typically <a href="../../../../../libs/multiprecision/doc/html/index.html" target="_top">Boost.Multiprecision</a>,
    </p>
<p>
      All of the following return a <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">cpp_bin_quad_float</span></code>
      result:
    </p>
<pre class="programlisting"><span class="identifier">cyl_bessel_j</span><span class="special">(</span><span class="number">0</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">cpp_bin_quad_float</span><span class="special">(</span><span class="number">2</span><span class="special">));</span>

<span class="identifier">cyl_bessel_j</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">cpp_bin_quad_float</span><span class="special">(</span><span class="number">2</span><span class="special">),</span> <span class="number">3</span><span class="special">);</span>

<span class="identifier">cyl_bessel_j</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">cpp_bin_quad_float</span><span class="special">(</span><span class="number">2</span><span class="special">),</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">multiprecision</span><span class="special">::</span><span class="identifier">cpp_bin_quad_float</span><span class="special">(</span><span class="number">3</span><span class="special">));</span>
</pre>
<p>
      but rely on the parameters provided being exactly representable, avoiding loss
      of precision from construction from <code class="computeroutput"><span class="keyword">double</span></code>.
    </p>
<div class="tip"><table border="0" summary="Tip">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Tip]" src="../../../../../doc/src/images/tip.png"></td>
<th align="left">Tip</th>
</tr>
<tr><td align="left" valign="top"><p>
        All new projects should use Boost.Multiprecision.
      </p></td></tr>
</table></div>
<p>
      During development of Boost.Math, <a href="http://www.shoup.net/ntl/" target="_top">NTL
      A Library for doing Number Theory</a> was invaluable to create highly precise
      tables.
    </p>
<p>
      All of the following return an <code class="computeroutput"><span class="identifier">NTL</span><span class="special">::</span><span class="identifier">RR</span></code> result:
    </p>
<pre class="programlisting"><span class="identifier">cyl_bessel_j</span><span class="special">(</span><span class="number">0</span><span class="special">,</span> <span class="identifier">NTL</span><span class="special">::</span><span class="identifier">RR</span><span class="special">(</span><span class="number">2</span><span class="special">));</span>

<span class="identifier">cyl_bessel_j</span><span class="special">(</span><span class="identifier">NTL</span><span class="special">::</span><span class="identifier">RR</span><span class="special">(</span><span class="number">2</span><span class="special">),</span> <span class="number">3</span><span class="special">);</span>

<span class="identifier">cyl_bessel_j</span><span class="special">(</span><span class="identifier">NTL</span><span class="special">::</span><span class="identifier">quad_float</span><span class="special">(</span><span class="number">2</span><span class="special">),</span> <span class="identifier">NTL</span><span class="special">::</span><span class="identifier">RR</span><span class="special">(</span><span class="number">3</span><span class="special">));</span>
</pre>
<p>
      In the last case, <code class="computeroutput"><span class="identifier">quad_float</span></code>
      is convertible to <code class="computeroutput"><span class="identifier">RR</span></code>, but not
      vice-versa, so the result will be an <code class="computeroutput"><span class="identifier">NTL</span><span class="special">::</span><span class="identifier">RR</span></code>. Note
      that this assumes that you are using a <a class="link" href="high_precision/use_ntl.html" title="Using NTL Library">patched
      NTL library</a>.
    </p>
<p>
      These rules are chosen to be compatible with the behaviour of <span class="emphasis"><em>ISO/IEC
      9899:1999 Programming languages - C</em></span> and with the <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1836.pdf" target="_top">Draft
      Technical Report on C++ Library Extensions, 2005-06-24, section 5.2.1, paragraph
      5</a>.
    </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2006-2021 Nikhar Agrawal, Anton Bikineev, Matthew Borland,
      Paul A. Bristow, Marco Guazzone, Christopher Kormanyos, Hubert Holin, Bruno
      Lalande, John Maddock, Evan Miller, Jeremy Murphy, Matthew Pulver, Johan Råde,
      Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg, Daryle
      Walker and Xiaogang Zhang<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="standalone.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../overview.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="error_handling.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
